﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using ERF;

namespace FirstPerson
{
    /// <summary>
    /// Represents a shader model 2.0 effect that is used for rendering skyboxes.
    /// Supports blending between two textures, for example to simulate.
    /// </summary>
    class SkyboxShader : EffectBase
    {
        private EffectParameter blendParameter;
        private EffectParameter shaderIndexParameter;
        private EffectParameter textureAParameter;
        private EffectParameter textureBParameter;

        private bool useBlend;

        /// <summary>
        /// Creates a new instance of the SkyboxShader.
        /// </summary>
        /// <param name="game">The game that this instance belongs to.</param>
        public SkyboxShader(ERF.ERFGame game)
            : base(game.GraphicsDevice, game.Content.Load<Effect>("Effects\\SkyboxShader")) {
            this.InitParameters();
        }

        /// <summary>
        /// Initializes the parameter fields by fetching the corresponding shader variable parameters.
        /// </summary>
        private void InitParameters() {
            this.blendParameter = this.Parameters["Blend"];
            this.shaderIndexParameter = this.Parameters["PixelShaderIndex"];
            this.textureAParameter = this.Parameters["TextureA"];
            this.textureBParameter = this.Parameters["TextureB"];
        }

        /// <summary>
        /// Updates the index that determines what PixelShader to use.
        /// </summary>
        private void UpdateShaderIndex()
        {
            if (this.useBlend)
                this.shaderIndexParameter.SetValue(0);
            else
                this.shaderIndexParameter.SetValue(1);
        }

        /// <summary>
        /// Gets or sets the value that determines how to blend the two textures used, if UseBlend is set to true.
        /// </summary>
        public float Blend
        {
            get { return this.blendParameter.GetValueSingle(); }
            set { this.blendParameter.SetValue(value); }
        }

        /// <summary>
        /// Gets or sets one of the two textures for the skybox.
        /// </summary>
        public Texture2D TextureA
        {
            get { return this.textureAParameter.GetValueTexture2D(); }
            set { this.textureAParameter.SetValue(value); }
        }

        /// <summary>
        /// Gets or sets one of the two textures for the skybox.
        /// </summary>
        public Texture2D TextureB
        {
            get { return this.textureBParameter.GetValueTexture2D(); }
            set { this.textureBParameter.SetValue(value); }
        }

        /// <summary>
        /// Gets or sets a value determining wether or not to blend between the two textures used.
        /// </summary>
        public bool UseBlend
        {
            get { return this.useBlend; }
            set
            {
                this.useBlend = value;
                this.UpdateShaderIndex();
            }
        }
    }
}
